<?php

declare(strict_types=1);

namespace app\admin\command;

use app\common\model\MatchesCategory;
use app\common\model\MatchesLeftCategory;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Category extends Command
{
    //分类 左侧类型数据拉去 爬取一次即可
    //主类型 里面包含竞彩数据  每天一次
    protected function configure()
    {
    
        // 指令配置
        $this->setName('category')
            ->addArgument('action', Argument::OPTIONAL, '参数')
            ->addOption('type', 't', Option::VALUE_OPTIONAL, '区别 1 主类型 2 左侧类型')
            ->addOption('source', 's', Option::VALUE_OPTIONAL, '类型')
            ->setDescription('分类');
    }

    protected function execute(Input $input, Output $output)
    {
        $action = trim($input->getArgument('action'));
        $source = trim($input->getOption('source'));
        $type = trim($input->getOption('type'));
        $timestamp = strtotime(date('Y-m-d', time()) . '00:00:00');
        if (intval($type) == 1) {
            $output->writeln('=========================开始赛事名称为:MatchesCategory=====================================');
            $is_find = MatchesCategory::field('id')->where(['type' => $action])->whereDay('owned_time', date('Y-m-d H:s:i', $timestamp))->find();
            $url = 'https://api-f.kqiu.cn/api/v1/' . $action . '/competition-filter?tab=0&timestamp=' . $timestamp;
            $res = curl_get($url);
            $arr = (json_decode($res, true));
            if ($arr['code'] == 0) {
                if (!empty($arr['data'])) {
                    $content = json_encode($arr['data'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                    $res_arr = ['type' => $action, 'content' => $content, 'owned_time' => $timestamp];
                    if (empty($is_find)) {
                        MatchesCategory::create($res_arr);
                        $str = 'add';
                    } else {
                        $is_find->save($res_arr);
                        $str = 'update';
                    }
                    $output->writeln('=========================' .$str. '完成=====================================');
                }
            }else{
                $output->writeln('MatchesCategory - api参数出错');
            }
        } else {
            $output->writeln('=========================开始赛事名称为:MatchesLeftCategory=====================================');
            $url = 'https://api-f.kqiu.cn/api/v1/' . $action . '/competition/' . $source . '-rank-keys';
            $res = curl_get($url);
            $arr = (json_decode($res, true));
            if ($arr['code'] == 0) {
                foreach ($arr['data'] as $key => $v) {
                    $is_find = MatchesLeftCategory::where(['type' => $action, 'source' => $source, 'key' => $v['key'], 'name' => $v['name']])->find();
                    $res_arr =  [
                        'name' => $v['name'],
                        'key' => $v['key'],
                        'type' => $action,
                        'source' => $source,
                    ];
                    if (empty($is_find)) {
                        MatchesLeftCategory::create($res_arr);
                        $str = 'add';
                    } else {
                        $is_find->save($res_arr);
                        $str = 'update';
                    }
                    $output->writeln('=========================循环第' . ($key + 1) . '个数据，' . $v['name'].',' .$str. '完成=====================================');
                }
            }else{
                $output->writeln('MatchesLeftCategory - api参数出错');
            }
        }

        // 指令输出
        $output->writeln('category');
    }
}
